<template>
  <div>
    <div class="action-bar">
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="add">新建</el-button>
      <!-- <el-button size="mini" icon="el-icon-download" @click="template">模板下载</el-button> -->
      <el-button size="mini" plain icon="el-icon-upload2" @click="uploadDialogFormVisible = true">导入</el-button>
    </div>
    <FormDialog title="新建供应商" :show="dialogFormVisible" width="50%" @on-ok="ok" @on-cancel="cancel">
      <EditForm ref="editForm" />
    </FormDialog>
    <FormDialog title="导入" :show="uploadDialogFormVisible" width="40%" @on-ok="closeDialog" @on-cancel="closeDialog">
      <UploadNavigation :download-funct="template" file-name="供应商模板" :action="action" :upload-success="handleSuccess" />
    </FormDialog>
  </div>
</template>
<script>
import FormDialog from '@neu/biz-core/components/FormDialog'
import UploadNavigation from '@neu/biz-core/components/UploadNavigation'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './editForm'
import { importExcelUrl, template } from '@neu/dcam-ui/api/manufacturer'

export default {
  name: 'ManufacturerActionBar',
  components: {
    FormDialog,
    EditForm,
    UploadNavigation
  },
  data() {
    return {
      dialogFormVisible: false,
      action: importExcelUrl,
      uploadDialogFormVisible: false,
      template: template
    }
  },
  methods: {
    // 新建供应商按钮点击处理
    add() {
      this.dialogFormVisible = true
    },
    // 创建供应商对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.$parent.refresh()
        // 关闭对话框
        this.dialogFormVisible = false
        this.$refs.editForm.reset()
      })
    },
    cancel() {
      this.$refs.editForm.reset()
      this.dialogFormVisible = false
    },
    handleSuccess(response) {
      this.uploadDialogFormVisible = false
      this.$parent.refresh()
    },
    closeDialog() {
      this.uploadDialogFormVisible = false
      this.$parent.refresh()
    }
  }

}
</script>
